package com.million.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.million.commonutils.R;
import com.million.eduservice.entity.EduComment;
import com.million.eduservice.entity.vo.comment.CommentVo;
import com.million.eduservice.entity.vo.comment.QueryComment;
import com.million.eduservice.service.EduCommentService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author 十麓
 * @since 2022-04-07
 */
@RestController
@RequestMapping("/eduservice/comment")
public class EduCommentController {

    @Autowired
    private EduCommentService eduCommentService;

    //根据课程id 分页查询课程评论信息
    @GetMapping("selectCommentPage/{page}/{limit}/{courseId}")
    private R selectCommentPage (@ApiParam(name = "page", value = "当前页码", required = true)
                                 @PathVariable("page") Long page,
                                 @ApiParam(name = "limit", value = "每页记录数", required = true)
                                 @PathVariable("limit") Long limit,
                                 @PathVariable("courseId") String courseId) {
        Page<EduComment> eduCommentPage = new Page<>(page, limit);
        Map<String, Object> map = eduCommentService.selectCommentPage(eduCommentPage, courseId);
        return R.ok().data(map);

    }

    //添加课程评论
    @PostMapping("addComment")
    public R addComment (@RequestBody EduComment eduComment, HttpServletRequest request) {
        String memberId = eduCommentService.addComment(eduComment, request);
        if (memberId == null) {
            return R.error().code(28004).message("请登录");
        }
        return R.ok();
    }

    //分页条件查询课程评论 渲染后台【评论功能
    @PostMapping("selectCommentVoPage/{page}/{limit}")
    public R selectCommentVoPage (@ApiParam(name = "page", value = "当前页码", required = true)
                                  @PathVariable("page") Long page,
                                  @ApiParam(name = "limit", value = "每页记录数", required = true)
                                  @PathVariable("limit") Long limit,
                                  @RequestBody QueryComment queryComment) {
        Page<CommentVo> commentVoPage = new Page<>(page,limit);
        Page<CommentVo> commentVoList =  eduCommentService.selectCommentVoPage(commentVoPage,queryComment);
        return R.ok().data("commentVoList",commentVoList);
    }

    //根据评论id 删除课程评论
    @DeleteMapping("deleteCommentById/{commentId}")
    public R deleteCommentById(@PathVariable("commentId") String commentId){
        eduCommentService.removeById(commentId);
        return R.ok();
    }


}

